<?php

namespace App\Http\Controllers\Admin;

use App\Model\Config;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ConfigController extends Controller
{
    public function edit($name){
        //测试hd_config函数
//        hd_config ('site.title');
        //hd_config ('site.title');
//        dd($name);
        //测试读取配置项数据
        //$res = Config::pluck('value','name');
        //dd($res->toArray());
        //测试读取缓存数据
        //$config = \Cache::get ('hd_config');
        //dd($config->toArray());

        //$data = Config::where('name',$name)->first();
        $data = Config::firstOrNew(['name'=>$name]);
        //dd($data->toArray());
        return view ('admin.config.edit_' . $name,compact ('name','data'));
    }
    public function update($name,Request $request){

        //dd($name);
        //dd($request->all ());
        $res = Config::updateOrCreate(
            ['name'=>$name],//查找条件
            ['name'=>$name,'value'=>$request->all ()]//添加更新的数据
        );
        //报错：Array to string
        //dd($res->toArray());
        hd_edit_env($request->all ());
        return back ()->with ('success','数据更新成功');
    }

}
